﻿using ClientAppraise.Views;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ClientAppraise.Services
{
    public class WindowsProviderService
    {
        private readonly IServiceProvider _serviceProvider;

        public WindowsProviderService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }


        public void Show<T>()
            where T : class
        {
            if (!typeof(Window).IsAssignableFrom(typeof(T)))
                throw new InvalidOperationException($"The window class should be derived from {typeof(Window)}.");


            if (_serviceProvider.GetService<T>() is not Window windowInstance)
            {
                throw new InvalidOperationException("Window is not registered as service.");
            }

            windowInstance.Owner = Application.Current.MainWindow;

            if (windowInstance is IWindow)
            {
                windowInstance.Loaded += WindowInstance_Loaded;
                windowInstance.Unloaded += WindowInstance_Unloaded;
            }

            windowInstance.Show();
        }

        public void ShowAsMain<T>() where T : class
        {
            if (_serviceProvider.GetService<T>() is not Window windowInstance)
            {
                throw new InvalidOperationException("Window is not registered as service.");
            }

            var mainWin = Application.Current.MainWindow;

            Application.Current.MainWindow = windowInstance;
            if (windowInstance is IWindow)
            {
                windowInstance.Loaded += WindowInstance_Loaded;
                windowInstance.Unloaded += WindowInstance_Unloaded;
                //windowInstance.Activated += WindowInstance_Activated;
            }

            windowInstance.Show();

            //Wpf.Ui.Appearance.ApplicationThemeManager.Apply(Wpf.Ui.Appearance.ApplicationTheme.Light);

            if (mainWin != windowInstance)//这里可以改改，如果相同时直接跳过，应该也是可以的
            {
                mainWin.Hide();
                mainWin.Close();
            }

        }

        private void WindowInstance_Unloaded(object sender, RoutedEventArgs e)
        {
            if (sender is IWindow window)
            {
                Wpf.Ui.Appearance.SystemThemeWatcher.UnWatch(((Window)sender));

                if (window.GetSnackbarPresenter() != default)
                {
                    App.GetRequiredService<IZxSnackbarService>()?.EjectContentPresenter(window.GetHashCode());
                }

                if (window.GetDialogPresenter() != default)
                {
                    App.GetRequiredService<IZxContentDialogService>()?.EjectContentPresenter(window.GetHashCode());
                }

                if (window.GetDrawerPresenter() != default)
                {
                    App.GetRequiredService<IZxDrawerService>()?.EjectContentPresenter(window.GetHashCode());
                }

                if (window.GetTitleRight() != default)
                {
                    App.GetRequiredService<IZxTitleRightService>()?.EjectContentPresenter(window.GetHashCode());
                }
            }
        }

        private void WindowInstance_Loaded(object sender, RoutedEventArgs e)
        {
            ((Window)sender).Loaded -= WindowInstance_Loaded;

            Wpf.Ui.Appearance.SystemThemeWatcher.Watch(((Window)sender));

            if (sender is IWindow window)
            {
                if (window.GetSnackbarPresenter() != default)
                {
                    App.GetRequiredService<IZxSnackbarService>()?.PushContentPresenter(window.GetHashCode(), window.GetSnackbarPresenter());
                }

                if (window.GetDialogPresenter() != default)
                {
                    App.GetRequiredService<IZxContentDialogService>()?.PushContentPresenter(window.GetHashCode(), window.GetDialogPresenter());
                }

                if (window.GetDrawerPresenter() != default)
                {
                    App.GetRequiredService<IZxDrawerService>()?.PushContentPresenter(window.GetHashCode(), window.GetDrawerPresenter());
                }

                if (window.GetTitleRight() != default)
                {
                    App.GetRequiredService<IZxTitleRightService>()?.PushContentPresenter(window.GetHashCode(), window.GetTitleRight());
                }
   
            }
        }
    }
}
